﻿using System;
using System.Linq;
using System.Text;
using System.IO;

namespace WordQuiz
{
    public abstract class WordQuizClient
    {
        public static Word[] ImportFile(string filePath)
        {
            string str;

            try
            {
                using (StreamReader sr = new StreamReader(@filePath, Encoding.Default))
                {
                    str = sr.ReadToEnd().Replace(Environment.NewLine, "");
                }

                string[] strArray = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                Word[] wordArray = new Word[strArray.Length];

                for (int i = 0; i < strArray.Length; i++)
                {
                    string[] word = strArray[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                    if (word.Length == 4)
                    {
                        wordArray[i] = new Word(word[0], word[1], word[2], word[3]);
                    }
                    if (word.Length == 3)
                    {
                        wordArray[i] = new Word(word[0], word[1], word[2]);
                    }
                    else if (word.Length == 2)
                    {
                        wordArray[i] = new Word(word[0], word[1]);
                    }
                    else
                    {
                        wordArray[i] = new Word(word[0]);
                    }
                }

                return wordArray;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public static void ExportFile(Word[] wordArray, string filePath, bool append = true)
        {
            string str = "";

            if (wordArray.Length > 0)
            {
                for (int i = 0; i < wordArray.Length; i++)
                {
                    if (wordArray[i].LanguageA != null && wordArray[i].LanguageA != "")
                    {
                        if (wordArray[i].IsCorrectAnswer != "")
                        {
                            str = str + string.Format("{0}:{1}:{2};{3};{4}", wordArray[i].LanguageA, wordArray[i].LanguageB, wordArray[i].Answer, wordArray[i].IsCorrectAnswer, Environment.NewLine);
                        }
                        if (wordArray[i].Answer != "")
                        {
                            str = str + string.Format("{0}:{1}:{2};{3}", wordArray[i].LanguageA, wordArray[i].LanguageB, wordArray[i].Answer, Environment.NewLine);
                        }
                        else if (wordArray[i].LanguageB != "")
                        {
                            str = str + string.Format("{0}:{1};{2}", wordArray[i].LanguageA, wordArray[i].LanguageB, Environment.NewLine);
                        }
                        else if (wordArray[i].LanguageA != "")
                        {
                            str = str + string.Format("{0};{1}", wordArray[i].LanguageA, Environment.NewLine);
                        }
                    }
                    else throw new ArgumentNullException();
                }

                try
                {
                    using (StreamWriter sw = new StreamWriter(@filePath, append, Encoding.Default))
                    {
                        sw.Write(str);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
            else throw new ArgumentNullException();
        }

        public struct Word
        {
            private string _languageA;
            private string _languageB;
            private string _answer;
            private string _isCorrectAnswer;

            public Word(string languageA, string languageB = "", string answer = "", string isCorrectAnswer = "")
                : this()
            {
                LanguageA = languageA;
                LanguageB = languageB;
                Answer = answer;
                IsCorrectAnswer = isCorrectAnswer;
            }

            public string LanguageA
            {
                get
                {
                    return _languageA;
                }
                private set
                {
                    if (value.Any(c => char.IsDigit(c)))
                    {
                        throw new FormatException();
                    }
                    else _languageA = value;
                }
            }
            public string LanguageB
            {
                get
                {
                    return _languageB;
                }
                private set
                {
                    if (value.Any(c => char.IsDigit(c)))
                    {
                        throw new FormatException();
                    }
                    else _languageB = value;
                }
            }
            public string Answer
            {
                get
                {
                    return _answer;
                }
                private set
                {
                    if (value.Any(c => char.IsDigit(c)))
                    {
                        throw new FormatException();
                    }
                    else _answer = value;
                }
            }
            public string IsCorrectAnswer
            {
                get
                {
                    return _isCorrectAnswer;
                }
                private set
                {
                    if (value == "R" || value == "F" || value == "")
                    {
                        _isCorrectAnswer = value;
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
            }

        }
    }
}
